﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;
using System.Collections;
using System.Collections.Specialized;

using EnvDTE;
using EnvDTE80;

namespace iMainAddin
{
    //public delegate String ConvertString(String myInt);
    public abstract class ExchangeDocument
    {
        Dictionary<string, iItem.Variables> m_ConvertDictionary = null;
        public bool Exchange(string SnippetInfoFileName, TextPoint StartPoint, TextPoint EndPoint)
        {
            if (!LoadVariables(SnippetInfoFileName))
                return false;

            return true;
        }
        public bool Exchange(Dictionary<string, iItem.Variables> stringDictionary, TextPoint StartPoint, TextPoint EndPoint)
        {

            if (stringDictionary == null)
                return false;

            m_ConvertDictionary = stringDictionary;
            return true;
        }
        private bool LoadVariables(string SnippetInfoFileName)
        {
            if (!System.IO.File.Exists(SnippetInfoFileName))
                return false;

            m_ConvertDictionary = new Dictionary<string, iItem.Variables>();
            System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument();

            xmlDoc.Load(SnippetInfoFileName);
            System.Xml.XmlNodeList nodes = xmlDoc.SelectNodes("/SnippetVariables/static");
            foreach (System.Xml.XmlNode node in nodes)
            {
                m_ConvertDictionary[node.Attributes.GetNamedItem("snippetvariable").Value]
                    = iItem.Variables.Create(node.Attributes.GetNamedItem("snippetvariable").Value, node.Attributes.GetNamedItem("value").Value);
            }
            return true;
        }
        // 컨버팅 명령어 추가
        public void AddDictionary(String key, iItem.Variables value)
        {
            if (m_ConvertDictionary == null)
            {
                m_ConvertDictionary = new Dictionary<string, iItem.Variables>();
            }
            if (m_ConvertDictionary.ContainsKey(key))
                return;
            m_ConvertDictionary.Add(key, value);
        }

        // 컨버팅 명령어 추가
        public void AddDictionary(String key, String value)
        {
            if (m_ConvertDictionary == null)
            {
                m_ConvertDictionary = new Dictionary<string, iItem.Variables>();
            }
            if (m_ConvertDictionary.ContainsKey(key))
                return;

            iItem.Variables newVariables = iItem.Variables.Create(value);
            m_ConvertDictionary.Add(key, newVariables);
        }

        public bool ChangeTextVariables(ref string srcText)
        {
            if (m_ConvertDictionary == null)
                return false;

            try
            {
                MatchCollection matches = Regex.Matches(srcText, "$(.*)");
                if (matches.Count > 0)
                {
                    foreach (Match match in matches)
                    {
                        IEnumerator myEnum = m_ConvertDictionary.GetEnumerator();
                        foreach (KeyValuePair<string, iItem.Variables> de in m_ConvertDictionary)
                        {
                            iItem.Variables convertVariables = de.Value as iItem.Variables;
                            if (convertVariables == null)
                                continue;

                            srcText = srcText.Replace(de.Key, convertVariables.Convert());
                        }
                    }
                }
            }
            catch
            {
                //Not going to trap anything...
                return false;
            }
            return true;
        }
    }
}
